package com.imooc.controller;

import com.google.gson.Gson;
import com.imooc.base.BaseInfoProperties;
import com.imooc.api.intercept.JWTCurrentUserInterceptor;
import com.imooc.pojo.Admin;
import com.imooc.pojo.Stu;
import com.imooc.pojo.Users;
import com.imooc.service.StuService;
import com.imooc.utils.SMSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("u")
@Slf4j
public class HelloController extends BaseInfoProperties {

    @Autowired
    private StuService stuService;
    @Autowired
    private SMSUtils smsUtils;

    @Value("${server.port}")
    private String port;

    @GetMapping("/hello")
    public Object hello(HttpServletRequest request) {

        String userJson = request.getHeader(APP_USER_JSON);
        Users jwtUser = new Gson().fromJson(userJson, Users.class);
        log.info("userJson:{}", jwtUser);

        Users users = JWTCurrentUserInterceptor.currentUser.get();
        log.info("users:{}", users);
        Admin admin = JWTCurrentUserInterceptor.adminUser.get();
        log.info("admin:{}", admin);

        Stu stu = new Stu();
        stu.setAge(18);
//        stu.setId(111);
        stu.setName("gqt");

        stuService.save(stu);
        log.info("lb测试,当前端口号为：" + port);

        return "OK";
    }

    @GetMapping("/sms")
    public Object sms() throws Exception {
        smsUtils.sendSMS("15688463267", "66666");

        return "OK";
    }
}
